import axios from "axios-miniprogram"
import { getStorage } from "./storage"

/*
*创建axios实例
*/
const request = axios.create({
  baseURL: 'https://gmall-prod.atguigu.cn/mall-api',
  timeout: 5000,
})
/*
*配置请求拦截器
*/
request.interceptors.request.use(config => {
  wx.showLoading({
    title: "数据加载中..."
  })
  const token = getStorage<string | undefined>("token")
  if (token) {
    config!.headers!.token = token
  }
  return config
}, error => {
  return Promise.reject(error)
})

/*
*配置响应拦截器
*/
request.interceptors.response.use(response => {
  wx.hideLoading()
  return response
}, error => {
  wx.hideLoading()
  return Promise.reject(error)
})

export default request